<?php
/**
 * @file: Redism.php
 * @User: shiwuhao
 * @Date: 16/3/22 17:01
 * @synopsis:
 * Redis 数据操作
 */

class Redism extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model('rssm');
        $this->load->model('type_weightm');
    }

    /**
     * 更新数据到缓存
     * @param $post array or id
     * @return bool
     */
    public function redis_update_rss_key($post)
    {
        if (is_array($post)) {
            $post_info = $post;
        } elseif (is_numeric($post)) {
            $post_info = $this->db->where(array('id' => $post, 'is_show' => 1))->limit(1)->get('ml_post')->row_array();
        } else {
            $post_info = array();
        }

        if(!$post_info) return false;

        $thread_list = $this->db->where(array('post_id' => $post_info['id'], 'count >='=>1, 'type' => 1))->get('ml_posttype')->result_array();

        if (!$thread_list) return false;

        if (in_array($post_info['post_type_extend'], array(0,1,2,3))) { // 兼容 2.1.3
            foreach($thread_list as $k => $v) {
                if (!$v['type_id']) continue;
                // addtime
                $key = RSS_THREAD_ADD_TIME_PREFIX.$v['type_id'];
                $res = $this->redis->zAdd($key, $post_info['addtime'], $post_info['id']);
                // visit_number
                $key = RSS_THREAD_VISIT_NUMBER_PREFIX.$v['type_id'];
                $this->redis->zAdd($key, $post_info['visit_number'], $post_info['id']);
                // comment_count
                $key = RSS_THREAD_COMMENT_COUNT_PREFIX.$v['type_id'];
                $this->redis->zAdd($key, $post_info['comment_count'], $post_info['id']);
                // favorite_count
                $key = RSS_THREAD_FAVORITE_COUNT_PREFIX.$v['type_id'];
                $this->redis->zAdd($key, $post_info['favorite_count'], $post_info['id']);
            }
        }

        return true;
    }

    /**
     * 更新 redis 中 帖子更新时间
     * @param $post_id
     * @param $addtime
     */
    public function update_post_addtime($post_id, $addtime)
    {
        $thread_list = $this->db->where(array('post_id' => $post_id, 'count >='=>1))->get('ml_posttype')->result_array();
        if (!$thread_list) {
            w_log('update_post_addtime_fail:'.$post_id);
            return false;
        }

        foreach($thread_list as $k => $v) {
            if (!$v['type_id']) continue;
            $key = RSS_THREAD_ADD_TIME_PREFIX.$v['type_id'];
            $res = $this->redis->zAdd($key, $addtime, $post_id);
            if (!$res) {
                w_log('zadd_update_post_addtime_fail:'.$post_id);
            }
        }

        return true;
    }

    /**
     * 更新 订阅集合中 添加时间 订阅排序
     * @param $post_id
     * @param $add_time
     */
    public function zset_add_member($post_id, $add_time)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            // addtime
            $key = RSS_THREAD_ADD_TIME_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, $add_time, $post_id);
            // visit_number
            $key = RSS_THREAD_VISIT_NUMBER_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, 0, $post_id);
            // comment_count
            $key = RSS_THREAD_COMMENT_COUNT_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, 0, $post_id);
            // favorite_count
            $key = RSS_THREAD_FAVORITE_COUNT_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, 0, $post_id);
        }

        // 添加 帖子计数信息
        $key = POST_INFO_PREFIX.$post_id;
        $this->redis->hSet($key, 'comment_total', 0);
        $this->redis->hSet($key, 'favorite_total', 0);
        $this->redis->hSet($key, 'visit_total', 0);
        return true;
    }

    /**
     * 删除 订阅集合中 指定的成员
     * @param $post_id
     * @return bool
     */
    public function zset_rem_member($post_id, $post_type_extend = 0)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;

        if (in_array($post_type_extend, array(0,1,2,3))) {
            foreach($thread_ids as $k => $v) {
                // addtime
                $key = RSS_THREAD_ADD_TIME_PREFIX.$v['type_id'];
                $res = $this->redis->zRem($key,$post_id);
                // visit_number
                $key = RSS_THREAD_VISIT_NUMBER_PREFIX.$v['type_id'];
                $res = $this->redis->zRem($key,$post_id);
                // comment_count
                $key = RSS_THREAD_COMMENT_COUNT_PREFIX.$v['type_id'];
                $res = $this->redis->zRem($key,$post_id);
                // favorite_count
                $key = RSS_THREAD_FAVORITE_COUNT_PREFIX.$v['type_id'];
                $res = $this->redis->zRem($key,$post_id);
            }
        }

        // 删除帖子计数信息
        $key = POST_INFO_PREFIX.$post_id;
        $this->redis->delete($key);
        return true;
    }

    /**
     * 更新评论数量 订阅排序
     * @param $post_id
     * @param $total
     */
    public function zset_update_comment_total($post_id, $total)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            $key = RSS_THREAD_COMMENT_COUNT_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, $total, $post_id);
        }
        return true;
    }

    /**
     * 更新收藏数量 订阅排序
     * @param $post_id
     * @param $total
     * @return bool
     */
    public function zset_update_favorite_total($post_id, $total)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            $key = RSS_THREAD_FAVORITE_COUNT_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, $total, $post_id);
        }
        return true;
    }

    /**
     * 更新浏览数量 订阅排序
     * @param $post_id
     * @param $total
     * @return bool
     */
    public function zset_update_visit_total($post_id, $total)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            $key = RSS_THREAD_VISIT_NUMBER_PREFIX.$v['type_id'];
            $this->redis->zAdd($key, $total, $post_id);
        }
        return true;
    }

    /**
     * 更新评论数量 增量 订阅排序
     * @param $post_id
     * @param $total
     */
    public function zset_incr_comment_total($post_id, $step = 1)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            $key = RSS_THREAD_COMMENT_COUNT_PREFIX.$v['type_id'];
            $this->redis->zIncrBy($key, $step, $post_id);
        }
        return true;
    }

    /**
     * 更新收藏数量 增量 订阅排序
     * @param $post_id
     * @param $total
     * @return bool
     */
    public function zset_incr_favorite_total($post_id, $step = 1)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            $key = RSS_THREAD_FAVORITE_COUNT_PREFIX.$v['type_id'];
            $this->redis->zIncrBy($key, $step, $post_id);
        }
        return true;
    }

    /**
     * 更新浏览数量 增量 订阅排序
     * @param $post_id
     * @param $total
     * @return bool
     */
    public function zset_incr_visit_total($post_id, $step = 1)
    {
        $thread_ids = $this->type_weightm->get_post_type_weight($post_id);
        if (!$thread_ids) return false;
        foreach($thread_ids as $k => $v) {
            $key = RSS_THREAD_VISIT_NUMBER_PREFIX.$v['type_id'];
            $this->redis->zIncrBy($key, $step, $post_id);
        }
        return true;
    }


    /**
     * 获取用户所订阅的科室下 帖子ID
     * @param array $thread_ids
     */
    public function get_rss_post_ids($user_id = 0,$page = 1, $limit = 10, $order_by = 'addtime')
    {
        if (!$user_id) return array();

        $this->load->model('rssm');
        $thread_ids = $this->rssm->get_user_rss_array($user_id);
        if (!$thread_ids) return array();

        switch ($order_by) {
            case 'visit_number':
                $key = RSS_THREAD_VISIT_NUMBER_PREFIX;
                break;
            case 'comment_count':
                $key = RSS_THREAD_COMMENT_COUNT_PREFIX;
                break;
            case 'favorite_count':
                $key = RSS_THREAD_FAVORITE_COUNT_PREFIX;
                break;
            default:
                $key = RSS_THREAD_ADD_TIME_PREFIX;
                break;
        }


        if (count($thread_ids) > 1) {
            // 取并集会生成新的key
            sort($thread_ids);
            $union_key = join('_',$thread_ids);
            $union_key = $key.$union_key;
            $is_existe = $this->redis->exists($union_key);
            if (!$is_existe) {
                $keys = array();
                foreach($thread_ids as $v){
                    $keys[] = $key.$v;
                }

                $total = $this->redis->zUnion($union_key, $keys, NULL, 'MAX');

                // 设置过期时间
                $this->redis->expire($union_key, RSS_THREAD_EXPIRE_TIME);
            }
        } else {
            $union_key = $key.$thread_ids[0];
        }

        $start = ($page - 1) * $limit;
        $end   = $start + $limit-1;

        return $this->redis->zRevRange($union_key, $start, $end);
    }

    /**
     * 更新hash 帖子信息
     * @param $post_id
     * @param array $params
     */
    public function hash_upadte_post_info($post_id, $params = array())
    {
        $key = POST_INFO_PREFIX.$post_id;

        if (isset($params['favorite_count']) && $params['favorite_count']) {
            $this->redis->hSet($key, 'favorite_total', $params['favorite_count']);
        }

        if (isset($params['comment_count']) && $params['comment_count']) {
            $this->redis->hSet($key, 'comment_total', $params['comment_count']);
        }

        if (isset($params['visit_count']) && $params['visit_count']) {
            $this->redis->hSet($key, 'visit_total', $params['visit_count']);
        }
        return TRUE;
    }

    // 设置，取消最佳评论日志记录
    public function accept_answer_log($key, $post_id, $uid, $comment_author_id, $comment_id)
    {
        $this->redis->hSet($key, 'set_answer_user', $uid);
        $this->redis->hSet($key, 'set_answer_post_id', $post_id);
        $this->redis->hSet($key, 'set_answer_comment_user', $comment_author_id);
        $this->redis->hSet($key, 'set_answer_comment', $comment_id);
        return true;

    }
}